package example;

import com.easyaop.api.Arounds;
import com.easyaop.core.AroundsImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.FrameworkServlet;

import java.util.UUID;

@SpringBootApplication
public class BApplication {
    static final String key = "Request-Id";

    public static void main(String[] args) throws Exception {
        AroundsImpl.getInstance().add(FrameworkServlet.class.getDeclaredMethod("processRequest", HttpServletRequest.class, HttpServletResponse.class),(the, argArr, join, target)->{
            try {
                HttpServletRequest request= (HttpServletRequest) argArr[0];
                String requestId = request.getHeader(key);
                MDC.put(key, requestId!=null?requestId:UUID.randomUUID().toString());
                return target.call();
            } finally {
                MDC.clear();
            }
        });
        SpringApplication.run(BApplication.class, args);
    }
}


